package com.hunglv.ppn.receiver;

import java.util.Calendar;

import com.hunglv.ppn.database.DatabaseHelper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		Object[] pdus = (Object[]) bundle.get("pdus");
		SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);

		String content = messages.getMessageBody();
		String phoneNumber = messages.getOriginatingAddress();
		Calendar calendar = Calendar.getInstance();

		DatabaseHelper mDatabaseHelper = new DatabaseHelper(context);
		mDatabaseHelper.open();
		mDatabaseHelper.insertNewLog(content, calendar, phoneNumber,
				DatabaseHelper.SMS_TYPE);
		mDatabaseHelper.close();

	}

}
